package cn.iocoder.yudao.module.system.controller.admin.ip;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.ip.core.utils.IPUtils;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 地区")
@RestController
@RequestMapping("/system/area")
@Validated
public class AreaController {

    @GetMapping("/tree")
    @Operation(summary = "获得地区树")
    public CommonResult<List<AreaNodeRespVO>> getAreaTree() {
        Area area = AreaUtils.getArea(Area.ID_CHINA);
        Assert.notNull(area, "获取不到中国");
        return success(BeanUtils.toBean(area.getChildren(), AreaNodeRespVO.class));
    }

    @GetMapping("/get-by-ip")
    @Operation(summary = "获得 IP 对应的地区名")
    @Parameter(name = "ip", description = "IP", required = true)
    public CommonResult<String> getAreaByIp(@RequestParam("ip") String ip) {
        // 获得城市
        Area area = IPUtils.getArea(ip);
        if (area == null) {
            return success("未知");
        }
        // 格式化返回
        return success(AreaUtils.format(area.getId()));
    }

    @GetMapping("/get-by-id")
    @Operation(summary = "获得 id 对应的城市名称")
    @Parameter(name = "ids", description = "IDS", required = true)
    public CommonResult<List<String>> getAreaById(@RequestParam("ids") String ids) {
        ArrayList<String> result = new ArrayList<>();
        List<Integer> itemList = Arrays.stream(ids.split(";"))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        if (!itemList.isEmpty()) {
            // 根据id获取城市名称,并且存放到result集合中
            itemList.forEach(id -> {
                result.add(AreaUtils.getArea(id).getName());
            });
        }
        // 格式化返回
        return success(result);
    }


    @GetMapping("/showCascader")
    @Operation(summary = "获取级联组件回数据")
    @Parameter(name = "pathStr", description = "城市id字符串", required = true)
    public CommonResult<List<List<Integer>>> showCascader(String pathStr) {
        ArrayList<List<Integer>> result = new ArrayList<>();
        // 处理字符串，使用;分割成数组，将数组转化为Integer类型的集合
        List<Integer> itemList = Arrays.stream(pathStr.split(";"))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        Area area = AreaUtils.getArea(Area.ID_CHINA);
        List<Area> cityTree = area.getChildren();
        // 遍历itemList中的每个元素, 找到对应的的地区
        for (Integer item : itemList) {
            cityTree.forEach(city -> {
                List<Area> children = city.getChildren();
                if (children != null) {
                    children.forEach(child -> {
                        if (child.getId().equals(item)) {
                            ArrayList<Integer> i = new ArrayList<>();
                            i.add(city.getId());
                            i.add(child.getId());
                            result.add(i);
                        }
                    });
                }
            });
        }
        Assert.notNull(area, "获取不到中国");
        return success(result);
    }
}
